package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.pojo.UserPurchase;
import com.example.demo.mapper.UserPurchaseMapper;
import com.example.demo.service.IUserPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户购买记录表 服务实现类
 * </p>
 *
 * @author gax
 * @since 2025-09-25
 */
@Service
public class UserPurchaseServiceImpl extends ServiceImpl<UserPurchaseMapper, UserPurchase> implements IUserPurchaseService {
    @Autowired
    private UserPurchaseMapper userPurchaseMapper;

    @Override
    public void buy(Long id, Long userId) {
        // 幂等：已购则直接返回
        long cnt = userPurchaseMapper.selectCount(
                new LambdaQueryWrapper<UserPurchase>()
                        .eq(UserPurchase::getUserId, userId)
                        .eq(UserPurchase::getVideoId, id)
        );
        if (cnt > 0) return;

        UserPurchase up = new UserPurchase();
        up.setUserId(userId);
        up.setVideoId(id);
        up.setPaidCents(100); // 演示为 100，真实可写价格
        userPurchaseMapper.insert(up);
    }
}
